var gulp     = require('gulp'),
    concat   = require('gulp-concat'), //- 多个文件合并为一个；
    cleanCSS = require('gulp-clean-css'), //- 压缩CSS为一行；
    ugLify   = require('gulp-uglify'), //压缩js
    imageMin = require('gulp-imagemin'), //压缩图片
    pngquant = require('imagemin-pngquant'), // 深度压缩
    htmlMin  = require('gulp-htmlmin'), //压缩html
    changed  = require('gulp-changed'), //检查改变状态(仅仅过滤修改的文件)
    less     = require('gulp-less'), //压缩合并less
    del      = require('del'), //删除文件
    browserSync = require("browser-sync").create();//浏览器实时刷新
    var babel = require("gulp-babel");

//npm init
//npm install --save-dev gulp gulp-concat gulp-clean-css gulp-uglify gulp-imagemin /n +
// imagemin-pngquant gulp-htmlmin gulp-changed gulp-less del browser-sync gulp-babel babel-preset-es2015 babel-core


//删除dist下的所有文件
gulp.task('delete',function(cb){
    // return del(['dist/*','!dist/images', '!dist/resource'],cb);
    return del(['dist/*'],cb);
});

//压缩html
gulp.task('html', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    gulp.src('src/*.html')
        .pipe(changed('dist', {hasChanged: changed.compareSha1Digest}))
        .pipe(htmlMin(options))
        .pipe(gulp.dest('dist'))
        .pipe(browserSync.reload({stream:true}));
});

//实时编译less 并导入dist/css
gulp.task('less', function () {
    gulp.src(['src/less/*.less']) //多个文件以数组形式传入
        .pipe(changed('dist/css', {hasChanged: changed.compareSha1Digest}))
        .pipe(less())//编译less文件
        .pipe(concat('main.css'))//合并之后生成main.css
        .pipe(cleanCSS())//压缩新生成的css
        .pipe(gulp.dest('dist/css'))//将会在css下生成main.css
        .pipe(browserSync.reload({stream:true}));
});

//导入 resource/css文件夹内容
gulp.task('css-mv', function () {
    gulp.src(['./src/resource/css/*.css'])
        .pipe(gulp.dest('dist/css'))
        .pipe(browserSync.reload({stream:true}));
});

//导入js
gulp.task('js-mv', function () {
    gulp.src(['./src/resource/js/*.js'])
        .pipe(gulp.dest('dist/js'))
        .pipe(browserSync.reload({stream:true}));
});

// es6转es5 并 压缩js
gulp.task("script",function(){
    gulp.src(['src/es6/*.js'])  // ES6 源码存放的路径
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(changed('dist/js', {hasChanged: changed.compareSha1Digest}))
        .pipe(concat('Gpagination.min.js'))
        .pipe(ugLify())
        .pipe(gulp.dest('dist/js'))
        .pipe(browserSync.reload({stream:true}));
});

// 压缩图片
gulp.task('images', function () {
    gulp.src('./src/images/*.*')
        .pipe(changed('dist/images', {hasChanged: changed.compareSha1Digest}))
        .pipe(imageMin({
            progressive: true,// 无损压缩JPG图片
            svgoPlugins: [{removeViewBox: false}], // 不移除svg的viewbox属性
            use: [pngquant()] // 使用pngquant插件进行深度压缩
        }))
        .pipe(gulp.dest('dist/images'))
        .pipe(browserSync.reload({stream:true}));
});

//启动热更新
gulp.task('serve', ['delete'], function() {
    gulp.start('script','less','css-mv','html','js-mv');
    browserSync.init({
        port: 2017,
        server: {
            baseDir: ['dist']
        }
    });
    gulp.watch('src/es6/*.js', ['script']);         //监控文件变化，自动更新
    gulp.watch('src/less/*.less', ['less']);
    gulp.watch('src/*.html', ['html']);
    gulp.watch('src/images/*.*', ['images']);
    gulp.watch('src/resource/css/*.*', ['css-mv']);
    gulp.watch('src/resource/js/*.*', ['js-mv']);
});

gulp.task('default',['serve']);